package org.phalanx.client.mapeditor.dialogs;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.border.TitledBorder;

import org.phalanx.client.UI.DialogManager;
import org.phalanx.client.UI.PhalanxIconButton;
import org.phalanx.client.UI.swing.PhalanxButtonFactory;
import org.phalanx.client.UI.swing.listeners.pButtonListener;
import org.phalanx.client.UI.swing.listeners.pButtonPressed;
import org.phalanx.client.mapeditor.PhalanxMapEditorPanel;
import org.phalanx.client.mapeditor.PhalanxMapTileSetViewer;
import org.phalanx.map.MapTile;
import org.phalanx.map.PhalanxMap;

public class DefaultTileSetDialog extends JFrame implements pButtonListener{
	private PhalanxMapEditorPanel masterPanel;
	DefaultTilePanel panel;
	public static final String NAME = "DEFAULTTILESETDIALOG";
	
	public DefaultTileSetDialog(PhalanxMapEditorPanel parentPanel){
		this.masterPanel = parentPanel;
		initializeSettings();
	}
	
	private void initializeSettings(){
		JFrame masterFrame = masterPanel.getMasterFrame();
		int pX = ((masterFrame.getWidth() + masterFrame.getLocation().x) + 
				masterFrame.getLocation().x) / 5;
		int pY = ((masterFrame.getHeight() + masterFrame.getLocation().y) + 
				masterFrame.getLocation().y) / 5;
		this.setSize(400, 400);
		this.setLocation(pX,pY);
		this.setTitle("Set Default Tile");
		panel = new DefaultTilePanel(this);
		this.add(panel);
		this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
	}
	
	private void applyDefaultTile(){
		PhalanxMap map = masterPanel.getDisplayContainer().getMapViewObject().getMap();
		MapTile oldDefaultTile = map.getDefaultTile();
		MapTile newDefaultTile = panel.getDefaultTile();

		if (newDefaultTile != null){
			for (int i = 0; i < map.getMapWidth(); i++){
				for (int j = 0; j < map.getMapLength(); j++){
					MapTile tempTile = map.getMapTileAt(i, j);

					if (tempTile.equals(oldDefaultTile)){
						map.setMapTile(i, j, newDefaultTile);
					}
				}
			}
		}
		
		map.setDefaultMapTile(newDefaultTile, false);
		masterPanel.getDisplayContainer().getMapViewObject().repaint();
	}
	
	public void actionPerformed(pButtonPressed p) {
		if (p.getComponent() instanceof PhalanxIconButton){
			PhalanxIconButton clickedButton = (PhalanxIconButton)p.getComponent();
			
			if (clickedButton.getName().equals("okbutton")){
				applyDefaultTile();
				closeWindow();
			}
			else if (clickedButton.getName().equals("cancel")) {
				closeWindow();
			}
		}
		
	}
	
	private void closeWindow() {
		DialogManager.getInstance().removeDialog(NAME);
		this.dispose();
	}



	class DefaultTilePanel extends JPanel {
		private static final long serialVersionUID = -303174894278365388L;
		PhalanxMapTileSetViewer viewer;
		PhalanxIconButton okButton;
		PhalanxIconButton cancelButton;
		DefaultTileSetDialog masterFrame;
		
		DefaultTilePanel(DefaultTileSetDialog parentFrame){
			masterFrame = parentFrame;
			initializePanel();
			setUpComponents();
			setUpListGrid();
			setUpBottomGrid();
		}
		
		private void initializePanel(){
			this.setBackground(Color.black);
			this.setForeground(Color.white);
			this.setLayout(new GridBagLayout());
		}
		
		private void setUpComponents(){
			okButton = PhalanxButtonFactory.getButton(PhalanxButtonFactory.DEFAULT_OK_BUTTON);
			okButton.setName("okbutton");
			
			cancelButton = PhalanxButtonFactory.getButton(PhalanxButtonFactory.DEFAULT_CANCEL_BUTTON);
			cancelButton.setName("cancel");
			
			okButton.addButtonListener(masterFrame);
			cancelButton.addButtonListener(masterFrame);
			
			TitledBorder b = BorderFactory.createTitledBorder("Tile List");
			b.setTitleColor(Color.white);
			
			viewer = new PhalanxMapTileSetViewer(masterPanel);
			viewer.setBorder(b);
			viewer.setCurrentMapTileSet(masterPanel.getDisplayContainer()
					.getMapViewObject().getMap().getMapTileSet());
		}
		
		private void setUpBottomGrid(){
			GridBagConstraints c = new GridBagConstraints();
			c.gridx = 0;
			c.gridy = 1;
			c.fill = GridBagConstraints.HORIZONTAL;
			
			JPanel containerPanel = new JPanel();
			containerPanel.setBackground(Color.black);
			containerPanel.setForeground(Color.white);
			containerPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
			
			containerPanel.add(okButton);
			containerPanel.add(cancelButton);
			
			this.add(containerPanel, c);
		}
		
		private void setUpListGrid(){
			GridBagConstraints c = new GridBagConstraints();
			c.gridx = 0;
			c.gridy = 0;
			c.fill = GridBagConstraints.BOTH;
			c.weightx = 0.5;
			c.weighty = 0.5;
			
			JPanel containerPanel = new JPanel();
			containerPanel.setBackground(Color.black);
			containerPanel.setForeground(Color.white);
			containerPanel.setLayout(new BoxLayout(containerPanel,BoxLayout.Y_AXIS));
			containerPanel.add(viewer);
			
			this.add(containerPanel, c);
		}
		
		protected MapTile getDefaultTile(){
			return viewer.getSelectedMapTile();
		}
	}
}
